package com.infoland.afanmall.base.view.refreshHeader;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.internal.ArrowDrawable;
import com.scwang.smartrefresh.layout.internal.ProgressDrawable;
import com.scwang.smartrefresh.layout.util.DensityUtil;

/**
 * @author 王永吉
 * @date 2018/7/27 13:53
 */
public class Header extends LinearLayout implements RefreshHeader {
    private View showAndCloseView;//
    private TextView mHeaderText;//标题文本
    private ImageView mArrowView;//下拉箭头
    private ImageView mProgressView;//刷新动画视图
    private ProgressDrawable mProgressDrawable;//刷新动画

    public Header(Context context) {
        this(context, null);
    }

    public Header(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        setGravity(Gravity.CENTER);
        mHeaderText = new TextView(context);
        mProgressDrawable = new ProgressDrawable();
        mArrowView = new ImageView(context);
        mProgressView = new ImageView(context);
        mProgressView.setImageDrawable(mProgressDrawable);
        mArrowView.setImageDrawable(new ArrowDrawable());
        addView(mProgressView, DensityUtil.dp2px(20), DensityUtil.dp2px(20));
        addView(mArrowView, DensityUtil.dp2px(20), DensityUtil.dp2px(20));
        addView(new Space(context), DensityUtil.dp2px(20), DensityUtil.dp2px(20));
        addView(mHeaderText, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        setMinimumHeight(DensityUtil.dp2px(60));
    }

    @NonNull
    public View getView() {
        return this;//真实的视图就是自己，不能返回null
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout layout, int height, int maxDragHeight) {
        mProgressDrawable.start();//开始动画
    }


    @Override
    public int onFinish(@NonNull RefreshLayout layout, boolean success) {
        mProgressDrawable.stop();//停止动画
        mProgressView.setVisibility(GONE);//隐藏动画
        ToolsKit.setTextColor(mHeaderText, "#000000");
        if (success) {
            mHeaderText.setText("刷新完成");
        } else {
            mHeaderText.setText("刷新失败");
        }
        return 500;//延迟500毫秒之后再弹回
    }


    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        ToolsKit.setTextColor(mHeaderText, "#000000");
        switch (newState) {
            case PullDownCanceled:
                showView();
//                LogKit.E("onStateChanged ---->   ", "  PullDownCanceled");
                break;
            case RefreshFinish:
//                showView();
//                LogKit.E("onStateChanged ---->   ", "  RefreshFinish");
                break;
            case None:
                showView();
//                LogKit.E("onStateChanged ---->   ", "  None");
                break;
            case PullDownToRefresh:
//                LogKit.E("onStateChanged ---->   ", "  PullDownToRefresh");
                closeView();
                mHeaderText.setText("下拉开始刷新");
                mArrowView.setVisibility(VISIBLE);//显示下拉箭头
                mProgressView.setVisibility(GONE);//隐藏动画
                mArrowView.animate().rotation(0);//还原箭头方向
                break;
            case Refreshing:
//                LogKit.E("onStateChanged ---->   ", "  Refreshing");
                mHeaderText.setText("正在刷新");
                mProgressView.setVisibility(VISIBLE);//显示加载动画
                mArrowView.setVisibility(GONE);//隐藏箭头
                break;
            case ReleaseToRefresh:
//                LogKit.E("onStateChanged ---->   ", "  ReleaseToRefresh");
                mHeaderText.setText("释放立即刷新");
                mArrowView.animate().rotation(180);//显示箭头改为朝上
                break;
            case PullUpToLoad:
//                LogKit.E("onStateChanged ---->   ", "  PullUpToLoad");
                break;
            case PullUpCanceled:
//                LogKit.E("onStateChanged ---->   ", "  PullUpCanceled");
                break;
            case ReleaseToLoad:
//                LogKit.E("onStateChanged ---->   ", "  ReleaseToLoad");
                break;
            case ReleaseToTwoLevel:
//                LogKit.E("onStateChanged ---->   ", "  ReleaseToTwoLevel");
                break;
            case TwoLevelReleased:
//                LogKit.E("onStateChanged ---->   ", "  TwoLevelReleased");
                break;
            case RefreshReleased:
//                LogKit.E("onStateChanged ---->   ", "  RefreshReleased");
                break;
            case LoadReleased:
//                LogKit.E("onStateChanged ---->   ", "  LoadReleased");
                break;
            case Loading:
//                LogKit.E("onStateChanged ---->   ", "  Loading");
                break;
            case TwoLevel:
//                LogKit.E("onStateChanged ---->   ", "  TwoLevel");
                break;

            case LoadFinish:
//                LogKit.E("onStateChanged ---->   ", "  LoadFinish");
                break;
            case TwoLevelFinish:
//                LogKit.E("onStateChanged ---->   ", "  TwoLevelFinish");
                break;
        }
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {

    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {

    }

//        @Override
//        public void onPulling(float percent, int offset, int height, int maxDragHeight) {
//
//        }
//        @Override
//        public void onReleasing(float percent, int offset, int height, int maxDragHeight) {
//
//        }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    private void closeView() {

        if (showAndCloseView != null) {
            LogKit.E("hideView ---->   ", "  hideView");
            showAndCloseView.setVisibility(GONE);
        }
    }

    private void showView() {
        if (showAndCloseView != null) {
            LogKit.E("showView ---->   ", "  showView");
            showAndCloseView.setVisibility(VISIBLE);
        }
    }

    public View getShowAndCloseView() {
        return showAndCloseView;
    }

    public void setShowAndCloseView(View showAndCloseView) {
        this.showAndCloseView = showAndCloseView;
    }
}
